import { defineComponent, watch, ref } from 'vue'
import * as echarts from 'echarts'
// 声明类型
const PropsType = {
  cdata: {
    type: Object,
    require: true
  }
} as const

// 定义主体
export default defineComponent({
  props: PropsType,
  setup(props) {
    // 定义 ref
    const chartRef = ref()
    // 配置项
    let options = {}

    // 监听
    watch(
      () => props.cdata,
      (val: any) => {
        options = {
          tooltip: {
            show: true,
            trigger: "item",
            axisPointer: {
              type: "shadow",
              label: {
                show: true,
                backgroundColor: "#7B7DDC"
              }
            }
          },
          legend: {
            show: false,
            // data: ['收入','支出']
          },
          grid: {
            x: "8%",
            width: "90%",
            top: "5%",
            height: '140px'
            // bottom: '7%'
          },
          xAxis: {
            data: val.category,
            type: 'category',
            // data: ['500元以下','500-1000元','1000-1500元', '1500-2000元', '2000-2500元', '2500-3000元', '3000元以上'],
            axisLine: {
              lineStyle: {
                color: "#B4B4B4"
              }
            },
            axisLabel:{
              fontSize:12,
              interval: 0,
              rotate: 30
            },
            axisTick: {
              show: false
            }
          },
          yAxis: [
            {
              type: 'value'
            }],
          series: [
            {
              name: "课程价格",
              type: "bar",
              barWidth: 10,
              itemStyle: {
                normal: {
                  barBorderRadius: 5,
                  color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    { offset: 0, color: "#956FD4" },
                    { offset: 1, color: "#3EACE5" }
                  ])
                }
              },
              data: val.barData
              // data: [1,2,3,4,5,6,7,8,9,10,11,12]
            }
          ]
        }
        // 手动触发更新
        if (chartRef.value) {
          // 通过初始化参数打入数据
          chartRef.value.initChart(options)
        }
      },
      {
        immediate: true,
        deep: true
      }
    )

    return () => {
      const height = "200px"
      const width = "100%"

      return <div>
        <echart ref={chartRef} height={height} width={width} />
      </div>
    }
  }
})

